<template>
  <div class="app-main-box">
    <!-- 历史记录表格 -->
    <Transition name="fade" mode="out-in">
      <div v-if="show_history_data == false">
        <div class="func-btn">
          <el-button round type="default" plain icon="Back" @click="$router.go(-1)">返回</el-button>
        </div>
        <div class="global-table-container" style="margin-top: 20px">
          <el-table class="global-border-radius-table" algin="center" v-loading="history_record_loading" :data="historyRecordList">
            <el-table-column label="序号" type="index" min-width="80" :index="indexMethod" />
            <el-table-column label="操作人" min-width="150" prop="operatorName" />
            <el-table-column label="操作时间" min-width="150" prop="createTime" />
            <el-table-column label="导入数量" min-width="150" prop="dataAmount" />
            <el-table-column fixed="right" label="操作" min-width="150" class-name="small-padding">
              <template #default="scope" min-width="200">
                <el-button link type="primary" icon="View" @click="viewHistoryData(scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 页码控件 -->
        <pagination v-show="history_record_total > 0" :total="history_record_total"
          v-model:page="queryParamsRecord.pageIndex" v-model:limit="queryParamsRecord.pageSize"
          :pageSizes="[15, 20, 30, 50]" @pagination="getHistoryRecordList" />
      </div>


      <!-- 历史数据 -->
      <div v-else>
        <div class="func-btn">
          <el-button round type="default" plain icon="Back" @click="show_history_data = false">返回</el-button>
        </div>

        <div v-if="operate_data" class="operate-info">
          操作人：{{ operate_data.operatorName }}<span style="margin: 0 40px;">操作时间：{{ operate_data.createTime
            }}</span>导入数量：{{
              operate_data.dataAmount }}
        </div>

        <!-- 表格数据 -->
        <div class="global-table-container">
          <el-table class="global-border-radius-table" algin="center" v-loading="loading" :data="historyData">
            <el-table-column label="车牌号" prop="plateNo" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="箱号" prop="containerNo" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column label="磅单号" prop="weightBillNo" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column label="过磅日期" prop="weightingDate" min-width="150" :show-overflow-tooltip="true" />
            <el-table-column label="货物名称" prop="cargo" min-width="150" :show-overflow-tooltip="true" />
            <el-table-column label="净重(吨)" prop="netWeight" min-width="100" :show-overflow-tooltip="true" />
            <el-table-column label="导入人" prop="creatorName" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="导入时间" prop="createTime" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column fixed="right" label="操作" class-name="small-padding" min-width="200">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="openDialog(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 页码控件 -->
          <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageIndex"
            v-model:limit="queryParams.pageSize" @pagination="getList" :pageSizes="[15, 20, 30, 50]" />
        </div>

        <el-dialog title="编辑运单" v-model="dialog_form_edit" width="800px" style="min-width: 750px" append-to-body>
          <!-- 编辑表单区域 -->
          <el-form class="global-form-edit"  :model="formEdit" ref="formEditRef"
            :rules="formEditRules" label-width="120px">
            <el-form-item label="车牌号：" prop="plateNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.plateNo" class="form-input" placeholder="请输入车牌号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="箱号：" prop="containerNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.containerNo" class="form-input" placeholder="请输入箱号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="磅单号：" prop="weightBillNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.weightBillNo" class="form-input" placeholder="请输入磅单号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="过磅日期：" prop="weightingDate" class="form-items">
              <el-date-picker  value-format="YYYY-MM-DD" v-model="formEdit.weightingDate" type="date"
                placeholder="请选择过磅日期" style="width: 100%;" clearable />
            </el-form-item>
            <el-form-item label="货物名称：" prop="cargo" class="form-items">
              <el-input v-noSpace v-model="formEdit.cargo" class="form-input" placeholder="请输入货物名称"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="净重(吨)：" prop="netWeight" class="form-items">
              <el-input v-noSpace v-model="formEdit.netWeight" class="form-input" placeholder="请输入净重" clearable>
                <template #append>吨</template>
              </el-input>
            </el-form-item>
          </el-form>
          <div class="dialog-footer" style="margin-top: 2px;">
            <el-button type="primary" @click="submitFormEdit">确 定</el-button>
            <el-button @click="cancelEdit">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </Transition>
  </div>


</template>

<script setup name="transportOrderList">
import { ElMessage } from 'element-plus'
import { getListApi, getHistoryListApi, updateListApi } from "@/api/transportOrderManagement/endShortBarge";
import { ref } from 'vue';
const router = useRouter();
const { proxy } = getCurrentInstance();
let show_history_data = ref(false)  // 点击历史记录列表详情展示历史数据
let dialog_form_edit = ref(false)
let loading = ref(true);
let history_record_loading = ref(false);
let operate_data = ref(null) // 历史操作信息数据
let total = ref(0);
let history_record_total = ref(0);
let dateRange = ref([]);
let historyData = reactive([]) // 历史数据
let historyRecordList = reactive([]) // 历史记录
/** 列表查询参数 */
const data = reactive({
  formEdit: {},
  formEditRules: {
    containerNo: [
      { required: true, message: "箱号不能为空", trigger: "blur" },
      // { max: 11, message: "箱号不能超过11位", trigger: "blur" }
    ],
    netWeight: [
      { required: true, message: "净重不能为空", trigger: "blur" },
      { pattern: /^(?!0(\.0+)?$)\d+(\.\d+)?$/, message: '仅允许输入大于0的小数和整数', trigger: 'blur' },
      { pattern: /^(?!0\d)(\d+(\.\d*)?|\.\d+)$/, message: '输入内容不合法', trigger: 'blur' }
    ],
    weightBillNo: [{ required: true, message: "磅单号不能为空", trigger: "blur" }],
    weightingDate: [{ required: true, message: "过磅日期不能为空", trigger: "blur" }],
    plateNo: [{ required: true, message: "车牌号不能为空", trigger: "blur" }],
    cargo: [{ required: true, message: "货物名称不能为空", trigger: "blur" }],
  },
  // 历史数据
  queryParams: {
    pageIndex: 1,
    pageSize: 15,

  },
  // 历史记录
  queryParamsRecord: {
    pageIndex: 1,
    pageSize: 15,
    historyImportId: '',
    waybillType: 3 // 到端
  }
});
const { queryParams, queryParamsRecord, formEdit, formEditRules } = toRefs(data);

/** 
* Methods
*/
/** 表格序号 */
function indexMethod(index) {
  index = index + 1
  if (queryParamsRecord.value.pageIndex > 1) {
    index = index + (queryParamsRecord.value.pageIndex - 1) * queryParamsRecord.value.pageSize
  }
  return index
}


/** 获取历史导入数据*/
function getList() {
  loading.value = true;
  let params = queryParams.value
  getListApi(proxy.addDateRange(params, dateRange.value, 'Date')).then(response => {
    if (response.code === 1) {
      historyData = response.data.records;
      total.value = Number(response.data.total);
      loading.value = false;
    } else {
      loading.value = false;
      // return false
    }
  }).catch(err => {
    loading.value = false;
    // return false
  })
}

/** 查看历史数据 */
async function viewHistoryData(row) {
  operate_data.value = row
  // 先清空原先的列表数据
  historyData = []
  show_history_data.value = true
  queryParams.value.historyImportId = row.id
  getList()
}

/** 获取历史记录 */
function getHistoryRecordList() {
  history_record_loading.value = true;
  const params = queryParamsRecord.value
  getHistoryListApi(proxy.addDateRange(params)).then(response => {
    if (response.code === 1) {
      historyRecordList = response.data.records;
      history_record_total.value = Number(response.data.total);
      history_record_loading.value = false;
    } else {
      history_record_loading.value = false;
    }
  }).catch(err => {
    history_record_loading.value = false;
  })
}


/** 打开编辑对话框 */
async function openDialog(row) {
  // 打开时重置表单
  resetEditForm()
  formEdit.value = { ...row }
  dialog_form_edit.value = true;
};

/** 提交表单 */
function submitFormEdit() {
  proxy.$refs["formEditRef"].validate(valid => {
    if (valid) {
      updateList()
    }
  })
}

/** 提交编辑铁路运单列表表单 */
function updateList() {
  let row = { ...formEdit.value, receiptDrayageId: formEdit.value.id }
  updateListApi(row).then(response => {
    if (response.code === 1) {
      updateTableRowData(response.data)
      dialog_form_edit.value = false
      ElMessage({ message: '操作成功', type: 'success' })
    } else {
      ElMessage({ message: response.message, type: 'error' })
    }
  }).catch(err => {
  })
}

/** 手动更新表格中某行的数据 */
function updateTableRowData(params) {
  historyData = historyData.map(item => {
    if (item.id.toString() == params.id.toString()) {
      item = Object.assign(item, params)
    }
    return item
  });
}

/** 编辑运单对话框-取消按钮 */
function cancelEdit() {
  dialog_form_edit.value = false;
  resetEditForm();
}

/** 重置编辑运单对话框 */
function resetEditForm() {
  formEdit.value = {
    weightBillNo: undefined,
    trainNo: undefined,
    containerNo: undefined,
    coverClothNo: undefined,
    waybillNo: undefined,
    cargo: undefined,
    weight: undefined,
    loadingDate: undefined,
    arrivalStation: undefined,
    departureStation: undefined,
    consignor: undefined,
    consignee: undefined
  }
  proxy.resetForm("formEditRef");
}

getHistoryRecordList()
</script>

<style lang='scss' scoped>
@import '../../../../assets/styles/variables.module.scss';

.app-main-box {
  background: #fff;
  border-radius: 12px;
  min-height: calc(100vh - 125px);
  width: calc(100% - 40px);

  /** 功能按钮 */
  .func-btn {
    padding: 24px 24px 0px;
  }

  .operate-info {
    text-align: center;
    font-size: 12px;
    color: $--color-info;
    margin: 20px 0px 14px;
  }


  .global-table-container {
    padding: 0px 24px;
  }
}

/** 导入说明文本 */
:deep(.el-upload__text) {
  width: 100%;
}

:deep(.el-upload-instructment) {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  .file-info {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: nowrap;
    width: 24em;

    .file-name {
      font-size: 18px;
      display: inline-block;
      flex: 0 1 auto;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .file-size {
      font-size: 14px;
      align-self: flex-end;
      color: $--color-info;
      display: inline-block;
      flex: 0 1 auto;
    }
  }

  .instructment-text {
    font-size: 18px;
    color: #333;
    font-weight: bold;
  }

  .suppose-type-text {
    display: inline-block;
    font-size: 12px;
    color: $--color-info;
    margin-top: 10px;
  }

  .download-template {
    span {
      font-size: 14px;
      color: $--color-primary;
      display: inline-block !important;
      padding: 10px;
    }
  }
}

:deep(.el-upload-dragger) {
  width: 100%;
  padding: 0px !important;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: transparent !important;
  border: none !important;
  border-radius: 0px !important;
}

:deep(.el-icon--upload) {
  margin-bottom: 0px !important;
}

/** 上传区域样式 */
.upload-content {
  border: 1px dashed $--color-info;
  background-color: $--color-bg-info !important;
  box-sizing: border-box !important;
  width: 100%;
  min-height: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 6px !important;
}

/** 上传失败区域 */
.upload-fail-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;

  .upload-fail-line {
    display: flex;
    align-items: center;

    .upload-fail-text {
      font-size: 20px;
      color: #333;
      font-weight: bold;
      margin-left: 14px;
    }
  }

  .upload-fail-instructment {
    color: $--color-info;
    margin-top: 14px;
  }

  .upload-fail-table {
    width: 100%;
    height: 240px;
    margin-top: 20px;

    .infinite-list-wrapper {
      width: 100%;
      height: 100%;
      border-radius: 6px;
      background-color: $--color-bg-info;
      padding: 0px 20px;
      overflow-y: auto;
    }

    .list {
      padding: 0;
      margin: 0;
      list-style: none;

      .list-item {
        display: flex;
        align-items: flex-end;
        height: 30px;
        line-height: 1;
        color: $--color-danger;
        white-space: nowrap;
      }
    }

  }
}

/** 隐藏上传的文本列表 */
:deep(.el-upload-list) {
  display: none;
}

/** 重新选择按钮 */
.toggle-btn {
  display: inline-block;
  margin-top: 14px;
}

/** 文件图标 */
.file-icon {
  width: 50px;
  height: 50px;
  color: #fff;
  font-size: 30px;
  font-style: italic;
  font-weight: bold;
  background-color: #00b42a; // 与字体主题色success一致
  border-radius: 4px;
  text-align: center;
  line-height: 50px;
  margin-bottom: 14px;
}

.upload-success-text {
  margin-top: 20px;
}


.dialog-footer {
  margin-top: 24px;
  text-align: right;
}
</style>